package com.cskaoyan.service.comment;

import com.cskaoyan.bean.MallComment;
import com.cskaoyan.bean.MallCommentExample;
import com.cskaoyan.mapper.MallCommentMapper;
import com.cskaoyan.model.bo.comment.CommentPageBo;
import com.cskaoyan.model.vo.comment.commentlist.CommentPageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: Jesson Guo
 * @create: 2021-12-01 21:59
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MallCommentMapper commentMapper;

    //    商品评论模块    展示所有评论
    @Override
    public CommentPageVO allCommentList(CommentPageBo commentPageBo) {
//    分页工具  需传参pageNum/limit
        PageHelper.startPage(commentPageBo.getPage(), commentPageBo.getLimit());
        MallCommentExample commentExample = new MallCommentExample();
//    判断是没有删除的评论
        MallCommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        commentExampleCriteria.andDeletedEqualTo(false);
//    判空后 精确查询 userId 和 valueId
        if (commentPageBo.getUserId() != null)
            commentExampleCriteria.andUserIdEqualTo(commentPageBo.getUserId());
        if (commentPageBo.getValueId() != null)
            commentExampleCriteria.andValueIdEqualTo(commentPageBo.getValueId());
//    按字段排序
        commentExample.setOrderByClause(commentPageBo.getSort() + " " + commentPageBo.getOrder());
        List<MallComment> comments = commentMapper.selectByExample(commentExample);
        PageInfo pageInfo = new PageInfo(comments);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        return new CommentPageVO(total, commentPageBo.getLimit(), commentPageBo.getPage(), comments);
    }

    //    商品评论模块    删除一条评论
    @Override
    public int deleteComment(MallComment mallComment) {
        //筛选条件
        MallCommentExample mallCommentExample = new MallCommentExample();
        MallCommentExample.Criteria commentExampleCriteria = mallCommentExample.createCriteria();
        //筛选id相同的评论 进行后续修改
        commentExampleCriteria.andIdEqualTo(mallComment.getId());
        mallComment.setDeleted(true);
        int i = commentMapper.updateByExampleSelective(mallComment, mallCommentExample);
        if (i == 1) return 200;
        else return 611;
    }
}
